using System;
using System.Collections.Generic;
using System.Threading;
using NUnit.Framework;
using Rhino.Mocks;
using StrategicDataSystems.Cloud.Monitoring.Configuration;
using StrategicDataSystems.Cloud.Monitoring.HostedServices;
using StrategicDataSystems.Cloud.Monitoring.UI;
using StrategicDataSystems.Cloud.Monitoring.UI.Services;
using StrategicDataSystems.Cloud.Monitoring.UI.ViewModels;

namespace StrategicDataSystems.Cloud.Monitoring.Tests.Given_A_SysTrayViewModel
{
    [TestFixture]
    public class When_Kill_All_Command_Is_Fired
    {
        [Test]
        public void Should_Call_To_Gateway_To_Kill_Each_Active_Deployment()
		{
		    var mockHostedServiceGateway = MockRepository.GenerateMock<IHostedServiceManagement>();
            var stubNetPresenceService = MockRepository.GenerateStub<INetPresenceService>();
		    var stubNotificationService = MockRepository.GenerateStub<INotificationService>();
		    var stubTimerService = MockRepository.GenerateStub<ITimerService>();

		    var fakeHostedServices = new List<HostedService>() { 
		            new HostedService("service1", new Uri("http://management.net"), 
		                new List<Deployment> () { new Deployment("Production", new Uri("http://exmaple.com"), "v1.0", "Running", "service1") } ),
		            new HostedService("service2", new Uri("http://management.net"),
		                new List<Deployment> () { new Deployment("Staging", new Uri("http://example.com"), "v1.2", "Stopped", "service2") } ) };

		    Settings settings = new Settings();

		    mockHostedServiceGateway.Stub(
		        x => x.GetHostedServicesForSubscription(settings.SubscriptionId, settings.CertificateThumbprint))
		        .Return(fakeHostedServices);
            stubNetPresenceService.Stub(x => x.InternetConnectionIsAvailable()).Return(true);

		    SysTrayViewModel viewModel = new SysTrayViewModel(mockHostedServiceGateway, settings, stubNotificationService, stubTimerService, stubNetPresenceService);

		    ((RelayCommand)viewModel.KillAllDeploymentsCommand).Execute(null);

            //Wait for background thread to complete
            Thread.Sleep(10);

		    mockHostedServiceGateway.AssertWasCalled(x => x.KillHostedServiceDeployment(TestSettings.TestSubscriptionId, TestSettings.TestCertificateThumbprint, "service1", "Production"));
		    mockHostedServiceGateway.AssertWasCalled(x => x.KillHostedServiceDeployment(TestSettings.TestSubscriptionId, TestSettings.TestCertificateThumbprint, "service2", "Staging"));
		}

        [Test]
        public void Should_Notify_User_Of_Kill_Commands_Sent()
        {
            var stubHostedServiceGateway = MockRepository.GenerateStub<IHostedServiceManagement>();
            var mockNotificationService = MockRepository.GenerateMock<INotificationService>();
            var stubNetPresenceService = MockRepository.GenerateStub<INetPresenceService>();
            var stubTimerService = MockRepository.GenerateStub<ITimerService>();

            var fakeHostedServices = new List<HostedService>() { 
		            new HostedService("service1", new Uri("http://management.net"), 
		                new List<Deployment> () { new Deployment("Production", new Uri("http://exmaple.com"), "v1.0", "Running", "service1") } ),
		            new HostedService("service2", new Uri("http://management.net"),
		                new List<Deployment> () { new Deployment("Staging", new Uri("http://example.com"), "v1.2", "Stopped", "service2") } ) };

            Settings settings = new Settings();

            stubHostedServiceGateway.Stub(
                x => x.GetHostedServicesForSubscription(settings.SubscriptionId, settings.CertificateThumbprint))
                .Return(fakeHostedServices);
            stubNetPresenceService.Stub(x => x.InternetConnectionIsAvailable()).Return(true);

            SysTrayViewModel viewModel = new SysTrayViewModel(stubHostedServiceGateway, settings, mockNotificationService, stubTimerService, stubNetPresenceService);

            ((RelayCommand)viewModel.KillAllDeploymentsCommand).Execute(null);

            //Wait for background thread to complete
            Thread.Sleep(10);


            mockNotificationService.AssertWasCalled(x => x.NotifyUserOfKillCommand(2));

        }

        [Test]
        public void Should_Alert_User_If_No_Internet_Connection_Is_Present()
        {
            var stubHostedServiceGateway = MockRepository.GenerateStub<IHostedServiceManagement>();
            var stubNetPresenceService = MockRepository.GenerateStub<INetPresenceService>();
            var mockNotificationService = MockRepository.GenerateMock<INotificationService>();
            var stubTimerService = MockRepository.GenerateStub<ITimerService>();

            Settings settings = new Settings();
            stubNetPresenceService.Stub(x => x.InternetConnectionIsAvailable()).Return(false);

            SysTrayViewModel viewModel = new SysTrayViewModel(stubHostedServiceGateway, settings, mockNotificationService, stubTimerService, stubNetPresenceService);

            ((RelayCommand)viewModel.KillAllDeploymentsCommand).Execute(null);

            mockNotificationService.AssertWasCalled(x => x.NotifyUserOfLostConnection());
        }
    }
}
